<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="试题编号" prop="questionid">
      <el-input v-model="dataForm.questionid" placeholder="试题编号"></el-input>
    </el-form-item>
    <el-form-item label="考试科目" prop="subject">
      <el-input v-model="dataForm.subject" placeholder="考试科目"></el-input>
    </el-form-item>
    <el-form-item label="问题题目" prop="question">
      <el-input v-model="dataForm.question" placeholder="问题题目"></el-input>
    </el-form-item>
    <el-form-item label="选项A" prop="answera">
      <el-input v-model="dataForm.answera" placeholder="选项A"></el-input>
    </el-form-item>
    <el-form-item label="选项B" prop="answerb">
      <el-input v-model="dataForm.answerb" placeholder="选项B"></el-input>
    </el-form-item>
    <el-form-item label="选项C" prop="answerc">
      <el-input v-model="dataForm.answerc" placeholder="选项C"></el-input>
    </el-form-item>
    <el-form-item label="选项D" prop="answerd">
      <el-input v-model="dataForm.answerd" placeholder="选项D"></el-input>
    </el-form-item>
    <el-form-item label="选项E" prop="answere">
      <el-input v-model="dataForm.answere" placeholder="选项E"></el-input>
    </el-form-item>
    <el-form-item label="选项F" prop="answerf">
      <el-input v-model="dataForm.answerf" placeholder="选项F"></el-input>
    </el-form-item>
    <el-form-item label="正确答案" prop="rightanswer">
      <el-input v-model="dataForm.rightanswer" placeholder="正确答案"></el-input>
    </el-form-item>
    <el-form-item label="题目解析" prop="analysis">
      <el-input v-model="dataForm.analysis" placeholder="题目解析"></el-input>
    </el-form-item>
    <el-form-item label="分数" prop="score">
      <el-input v-model="dataForm.score" placeholder="分数"></el-input>
    </el-form-item>
    <el-form-item label="所属章节" prop="section">
      <el-input v-model="dataForm.section" placeholder="所属章节"></el-input>
    </el-form-item>
    <el-form-item label="难度等级" prop="level">
      <el-input v-model="dataForm.level" placeholder="难度等级"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          questionid: '',
          subject: '',
          question: '',
          answera: '',
          answerb: '',
          answerc: '',
          answerd: '',
          answere: '',
          answerf: '',
          rightanswer: '',
          analysis: '',
          score: '',
          section: '',
          level: ''
        },
        dataRule: {
          questionid: [
            { required: true, message: '试题编号不能为空', trigger: 'blur' }
          ],
          subject: [
            { required: true, message: '考试科目不能为空', trigger: 'blur' }
          ],
          question: [
            { required: true, message: '问题题目不能为空', trigger: 'blur' }
          ],
          answera: [
            { required: true, message: '选项A不能为空', trigger: 'blur' }
          ],
          answerb: [
            { required: true, message: '选项B不能为空', trigger: 'blur' }
          ],
          answerc: [
            { required: true, message: '选项C不能为空', trigger: 'blur' }
          ],
          answerd: [
            { required: true, message: '选项D不能为空', trigger: 'blur' }
          ],
          // answere: [
          //   { required: true, message: '选项E不能为空', trigger: 'blur' }
          // ],
          // answerf: [
          //   { required: true, message: '选项F不能为空', trigger: 'blur' }
          // ],
          rightanswer: [
            { required: true, message: '正确答案不能为空', trigger: 'blur' }
          ],
          analysis: [
            { required: true, message: '题目解析不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '分数不能为空', trigger: 'blur' }
          ],
          section: [
            { required: true, message: '所属章节不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '难度等级不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/exam/multiquestion/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.questionid = data.multiQuestion.questionid
                this.dataForm.subject = data.multiQuestion.subject
                this.dataForm.question = data.multiQuestion.question
                this.dataForm.answera = data.multiQuestion.answera
                this.dataForm.answerb = data.multiQuestion.answerb
                this.dataForm.answerc = data.multiQuestion.answerc
                this.dataForm.answerd = data.multiQuestion.answerd
                this.dataForm.answere = data.multiQuestion.answere
                this.dataForm.answerf = data.multiQuestion.answerf
                this.dataForm.rightanswer = data.multiQuestion.rightanswer
                this.dataForm.analysis = data.multiQuestion.analysis
                this.dataForm.score = data.multiQuestion.score
                this.dataForm.section = data.multiQuestion.section
                this.dataForm.level = data.multiQuestion.level
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/exam/multiquestion/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'questionid': this.dataForm.questionid,
                'subject': this.dataForm.subject,
                'question': this.dataForm.question,
                'answera': this.dataForm.answera,
                'answerb': this.dataForm.answerb,
                'answerc': this.dataForm.answerc,
                'answerd': this.dataForm.answerd,
                'answere': this.dataForm.answere,
                'answerf': this.dataForm.answerf,
                'rightanswer': this.dataForm.rightanswer,
                'analysis': this.dataForm.analysis,
                'score': this.dataForm.score,
                'section': this.dataForm.section,
                'level': this.dataForm.level
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
